#!/bin/bash

# days_since_epoch : the days since the 1970/1/1
function days_since_epoch() {
  readonly epoch_days=719591

  declare -a curDATE=( `date +'%d %m %Y'` )
  local day=${curDATE[0]}
  local month=$(echo ${curDATE[1]} | sed 's/0*//')
  local year=${curDATE[2]}

  if [[ ${month} -gt 2 ]]; then
    month=$((${month} + 1))
  else
    month=$((${month} + 13))
    year=$((${year} - 1))
  fi

  local today_days=$(((${year} * 365) + (${year} / 4) - (${year} / 100) + (${year} / 400) + (${month} * 306001 / 10000) + ${day}))
  local days_since_epoch=$((${today_days} - ${epoch_days}))
  echo ${days_since_epoch}
  return 0
}

# seconds_since_epoch : the number of second from the 1910/1/1
function seconds_since_epoch() {
  declare -a curTIME=( `date +'%S %M %k'` )
  local second=${curTIME[0]}
  local minute=${curTIME[1]}
  local hour=${curTIME[2]}

  local days=$(days_since_epoch)
  local seconds_since_epoch=$(((${days} * 86400) + (${hour} * 3600) + (${minute} * 60) + ${second}))
  echo ${seconds_since_epoch}
  return 0
}
